<!DOCTYPE html>
<html>
<head>
    <title>Simulating DOM Keyboard Events Example</title>
    <script type="text/javascript" src="EventUtil.js"></script>
</head>
<body>
    <input type="text" value="" id="myTextbox" />
    <input type="button" value="Send keydown to the textbox" id="myBtn" />
    <script type="text/javascript">
    
    (function(){
        var btn = document.getElementById("myBtn");
        var textbox = document.getElementById("myTextbox");
        
        EventUtil.addHandler(textbox, "keydown", function(event){
            alert(event.type);
            alert(event.keyCode);
        });

        EventUtil.addHandler(btn, "click", function(event){

            //create event object
            var event;
            
            //DOM Level 3 implementation
            if (document.implementation.hasFeature("KeyboardEvent", "3.0")){
                event = document.createEvent("KeyboardEvent");
                event.initKeyboardEvent("keydown", true, true, "a", 0, "Shift");            
            } else {
                try {
                    //Firefox implementation
                    event = document.createEvent("KeyEvents");
                    event.initKeyEvent("keydown", true, true, document.defaultView, false, false, 
                                        true, false, 65, 65);    
                } catch (ex){ //others
                    event = document.createEvent("Events");
                    event.initEvent("keydown", true, true);
                    event.view = document.defaultView;
                    event.altKey = false;
                    event.ctrlKey = false;
                    event.shiftKey = false;
                    event.metaKey = false;
                    event.keyCode = 65;
                    event.charCode = 65;            
                }               
                
                
            }
            
         
            
            //fire the event
            textbox.dispatchEvent(event);

        });

    })();
    </script>
</body>
</html>
